package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.OverflowDao;
import com.atguigu.jxc.dao.OverflowGoodsDao;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OverflowServiceImpl implements OverflowService {

    @Autowired
    private OverflowDao overflowDao;
    @Autowired
    private OverflowGoodsDao overflowGoodsDao;

    @Override
    public void save(OverflowList overflowList, String overflowListGoodsStr) {
        overflowDao.insert(overflowList);
        List<OverflowListGoods> overflowListGoods = JSONArray.parseArray(overflowListGoodsStr, OverflowListGoods.class);
        for (OverflowListGoods overflowListGood : overflowListGoods) {
            Integer overflowListId = overflowList.getOverflowListId();
            overflowListGood.setOverflowListId(overflowListId);
            overflowGoodsDao.insert(overflowListGood);
        }
    }

    @Override
    public Map<String, Object> getOverflowList(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowList> overflowLists =  overflowDao.selectOverflowList(sTime,eTime);
        map.put("rows",overflowLists);
        return map;
    }

    @Override
    public Map<String, Object> getOverflowGoodsList(Integer overflowListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowListGoods> overflowListGoodsList =  overflowGoodsDao.selectOverflowGoodsList(overflowListId);
        map.put("rows",overflowListGoodsList);
        return map;
    }
}
